import request from '@/utils/request'

export function login(data) {
  return new Promise((resolve, reject) => {
    request({
      url: "/api/user/login",
      method: "post",
      data: {
        username: data.username,
        password: data.password
      }
    }).then(resp => {
      if(resp.success) {
        resolve(resp.data);
      } else {
        reject(resp);
      }
    })
  });

  // return new Promise(resolve => {
  //   resolve({ code: 20000, data: { token: 'admin-token' }})
  // })
  // return request({
  //   url: '/vue-element-admin/user/login',
  //   method: 'post',
  //   data
  // })
}

export function getInfo(token) {
  return new Promise((resolve, reject) => {
    request({
      url: "/api/user/userInfo",
    }).then(resp => {
      if(resp.success) {
        resolve({
          ...resp.data,
          roles: ['admin'],
          name: '管理员',
          avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
          introduction: 'I am a super administrator'
        });
      } else {
        reject(resp.message);
      }
    })
  });

  // return new Promise(resolve => {
  //   return resolve({
  //     'code': 20000,
  //     'data': {
  //       'roles': [
  //         'admin'
  //       ],
  //       'introduction': 'I am a super administrator',
  //       'avatar': 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
  //       'name': 'Super Admin'
  //     }
  //   })
  // })

  // return request({
  //   url: '/vue-element-admin/user/info',
  //   method: 'get',
  //   params: { token }
  // })
}

export function logout() {
  return request({
    url: '/api/user/logout',
    method: 'post'
  })
}
